package br.moneymonitor.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import br.moneymonitor.activity.R;

public class DataBaseHelper extends SQLiteOpenHelper {
	
	private Context context;
	private SQLiteDatabase db;
	
	private static final int[] createTables = {
		R.string.dbCreateModule,
		R.string.dbCreateAppointment,
		R.string.dbCreateModuleFields,
		R.string.dbCreateUser
	};
	
	private static final int[] dropTables = {
		R.string.dbDropAppointment,
		R.string.dbDropModule,
		R.string.dbDropModuleFields,
		R.string.dbDropUser
	};
	
	public DataBaseHelper(Context context, String database, int version) {
		super(context, database, null, version);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db; 
		String sql = null;
		for (int script:createTables) {
			sql = context.getString(script);
			db.execSQL(sql);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = null;
		for (int script:dropTables) {
			sql = context.getString(script);
			db.execSQL(sql);
		}
		onCreate(db);
	}
	
	public void close(){
		if(db != null){
			db.close();
		}
	}
}
